#!/usr/bin/python
def headerPrint():
    print '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /><title>Original ~ Shorten those long urls</title><link rel="stylesheet" href="styles.css" type="text/css" /></head><body><div id="wrap">	<div class="header">		<div class="title">			<h1><a href="http://origin.al.eu.org">Original</a></h1>			<h2>Python URL Shortening Service</h2>		</div>		<div class="nav">			<ul>				<li><a href="index.html">Home</a></li>			</ul>		</div>		<div class="clear"></div>	</div>	<div class="pagewrapper">		<div class="innerpagewrapper">			<div class="page">				<div class="content">					<p>'
def footerPrint():
    print '</p>				</div>				<div class="footer">					<p>&copy;&nbsp;2010&nbsp;<a href="http://origin.al.eu.org">Origin.al.eu.org</a>|Design: <a href="http://www.spyka.net">spyka webmaster</a></p> 				</div>			</div>		</div>	</div></div></body></html>'
#module checks
print 'Content-type: text/html\n\n'
headerPrint()
try:
    import MySQLdb
    from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
    import cgi, cgitb
except ImportError, e:
    print "<font color='red'>These modules need to be installed</font>"
    print "<font color='red'>%s</font>" % (e.args[0])
    exit()
else:
    print "<font color='green'>Module Checks Completed</font><br/>"
import MySQLdb
from SimpleXMLRPCServer import CGIXMLRPCRequestHandler
import cgi, cgitb
form = cgi.FieldStorage()
#GET cgi fields
dbHost = form.getvalue('dbHost')
dbName = form.getvalue('dbName')
dbUserName = form.getvalue('dbUserName')
dbPassword = form.getvalue('dbPassword')
SmtpHost = form.getvalue('SmtpHost')
SmtpPort = form.getvalue('SmtpPort')
SmtpUserName = form.getvalue('SmtpUserName')
SmtpPassword = form.getvalue('SmtpPassword')
url = form.getvalue('url')
fromAddress = form.getvalue('fromAddress')
#write config file
try:
    f = open('./config.py', 'w')
    f.write("class config:\n")
    f.write("    #database settings\n")
    f.write("    dbUsername = \""+dbUserName+"\"\n")
    f.write("    dbPassword = \""+dbPassword+"\"\n")
    f.write("    dbHostname = \""+dbHost+"\"\n")
    f.write("    dbName = \""+dbName+"\"\n")
    f.write("    #url and domain settings\n")
    f.write("    url = \""+url+"\"\n")
    f.write("    #smtp settings\n")
    f.write("    fromAddress = \""+fromAddress+"\"\n")
    f.write("    smtpHost = \""+SmtpHost+"\"\n")
    f.write("    smtpPort = "+SmtpPort+"\n")
    f.write("    userName = \""+SmtpUserName+"\"\n")
    f.write("    password = \""+SmtpPassword+"\"")
except Exception, e:
    print "<font color='red'>%s</font>" % (e.args[0])
    exit()
else:
    print "<font color='green'>Configuration file written withour errors...</font><br />"
#Database installation
try:
    db = MySQLdb.connect(host=dbHost,user=dbUserName,passwd=dbPassword,db=dbName)
    sql = """CREATE TABLE `link_database` (`id` BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY ,`hash` BIGINT NOT NULL ,`link` TEXT NOT NULL ,`ipaddress` VARCHAR( 16 ) NOT NULL)ENGINE = MYISAM ;"""
    db.query(sql)
    sql = """CREATE TABLE `link_database_register`  (`id` INT NOT NULL AUTO_INCREMENT ,`email` VARCHAR( 250 ) NOT NULL ,`passwd` VARCHAR( 4 ) NOT NULL ,`apikey` TEXT NOT NULL,  `ipaddress` VARCHAR( 16 ) NOT NULL ,PRIMARY KEY (`id` ) ,UNIQUE (`email`)) ENGINE = MYISAM ;"""
    db.query(sql)
except MySQLdb.Error, e:
    print e
    exit()
else:
    print "<font color='green'>Database Install Successful...</font>"
footerPrint()